home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 414_02 / demos / newdemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-17  |  8.9 KB  |  353 lines

  1. /* $Header: C:\CURSES\demos\RCS\newdemo.c 2.1 1993/06/18 20:24:09 MH Rel MH $
  2.  *
  3.  *  newdemo.c    -    A demo program using PDCurses. The program illustrate
  4.  *               the use of colours for text output.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <signal.h>
  9. #include <time.h>
  10. #include <curses.h>
  11.  
  12. #ifdef PDCDEBUG
  13. char *rcsid_newdemo = "$Header: C:\CURSES\demos\RCS\newdemo.c 2.1 1993/06/18 20:24:09 MH Rel MH $";
  14. #endif
  15.  
  16. /*
  17.  *  The Australian map
  18.  */
  19. char    *AusMap[16] =
  20. {
  21.     "           A           A ",
  22.     "    N.T. AAAAA       AAAA ",
  23.     "     AAAAAAAAAAA  AAAAAAAA ",
  24.     "   AAAAAAAAAAAAAAAAAAAAAAAAA Qld.",
  25.     "AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
  26.     "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
  27.     " AAAAAAAAAAAAAAAAAAAAAAAAAAAA ",
  28.     "   AAAAAAAAAAAAAAAAAAAAAAAAA N.S.W.",
  29.     "W.A. AAAAAAAAA      AAAAAA Vic.",
  30.     "       AAA   S.A.     AA",
  31.     "                       A  Tas.",
  32.     ""
  33. };
  34.  
  35. /*
  36.  *  Funny messages
  37.  */
  38. #define NMESSAGES   6
  39.  
  40. char    *messages[] =
  41. {
  42.     "Hello from the Land Down Under",
  43.     "The Land of crocs. and a big Red Rock",
  44.     "Where the sunflower runs along the highways",
  45.     "the dusty red roads lead one to loneliness",
  46.     "Blue sky in the morning and",
  47.     "freezing nights and twinkling stars",
  48.     ""
  49. };
  50.  
  51. /*
  52.  *  Main driver
  53.  */
  54. main()
  55. {
  56. WINDOW  *win;
  57. int     w, x, y, i, j, c, len;
  58. time_t  t;
  59. char    buffer[80], *message;
  60. int     width, height;
  61. chtype  save[80];
  62. void    trap();
  63.  
  64.     initscr();
  65.     start_color();
  66.     cbreak();
  67.     signal(SIGINT, trap);
  68.     width  = 48;
  69.     height = 14;                /* Create a drawing window */
  70.     win = newwin(height, width, (LINES-height)/2, (COLS-width)/2);
  71.     if(win == NULL)
  72.     {   endwin();
  73.         return 1;
  74.     }
  75.  
  76.     while(1)
  77.     {   init_pair(1,COLOR_WHITE,COLOR_BLUE);
  78.         wattrset(win, COLOR_PAIR(1));
  79.         werase(win);
  80.  
  81.         init_pair(2,COLOR_RED,COLOR_RED);
  82.         wattrset(win, COLOR_PAIR(2));
  83.         box(win, ACS_VLINE, ACS_HLINE);
  84.         wrefresh(win);
  85.                                 /* Do ramdom output of a character */
  86.         wattrset(win, COLOR_PAIR(1));
  87.         c = 'a';
  88.         for(i=0; i < 5000; ++i)
  89.         {   x = rand() % (width-2)  + 1;
  90.             y = rand() % (height-2) + 1;
  91.             mvwaddch(win, y, x, c);
  92.             wrefresh(win);
  93.             nodelay(win,TRUE);
  94.             if (wgetch(win) != ERR)
  95.                 break;
  96.             if(i == 2000)
  97.             {   c = 'b';
  98.                 init_pair(3,COLOR_CYAN,COLOR_YELLOW);
  99.                 wattron(win, COLOR_PAIR(3));
  100.             }
  101.         }
  102.  
  103.         SubWinTest(win);
  104.                                 /* Erase and draw green window */
  105.         init_pair(4,COLOR_YELLOW,COLOR_GREEN);
  106.         wattrset(win, COLOR_PAIR(4) | A_BOLD);
  107.         werase(win);
  108.         wrefresh(win);
  109.                                 /* Draw RED bounding box */
  110.         wattrset(win, COLOR_PAIR(2));
  111.         box(win, ' ', ' ');
  112.         wrefresh(win);
  113.                                 /* Display Australia map */
  114.     wattrset(win, COLOR_PAIR(4) | A_BOLD);
  115.         i = 0;
  116.         while(*AusMap[i])
  117.     {   mvwaddstr(win, i+1, 8, AusMap[i]);
  118.             wrefresh(win);
  119.             delay_output(100);
  120.             ++i;
  121.         }
  122.  
  123.         init_pair(5,COLOR_BLUE,COLOR_WHITE);
  124.         wattrset(win, COLOR_PAIR(5) | A_BLINK);
  125.     mvwaddstr(win, height-2, 6, " PDCurses 2.1 for DOS, OS/2 and Unix");
  126.     wrefresh(win);
  127.  
  128.                 /* Draw running messages */
  129.     init_pair(6,COLOR_YELLOW,COLOR_WHITE);
  130.     wattrset(win, COLOR_PAIR(6));
  131.     message = messages[0];
  132.     len = strlen(message);
  133.     j = 0;
  134.     i = 2;
  135.     w = width-2;
  136.         while(j < NMESSAGES)
  137.         {   strncpy(buffer, message, w - i);
  138.             buffer[w-i] = 0;
  139.         mvwaddstr(win, height/2, i, buffer);
  140.             if(w - i < len)
  141.             {   memset(buffer, ' ', i);
  142.                 strcpy(buffer, message + (w - i));
  143.                 buffer[strlen(buffer)]   = ' ';
  144.                 buffer[i-2] = '\0';
  145.                 mvwaddstr(win, height/2, 2, buffer);
  146.         }
  147.             wrefresh(win);
  148.             nodelay(win,TRUE);
  149.             if (wgetch(win) != ERR)
  150.             {   flushinp();
  151.         break;
  152.             }
  153.             mvwaddch(win, height/2, i, ' ');
  154.             i = ++i % w;
  155.             if(i < 2)
  156.             {   message = messages[++j%NMESSAGES];
  157.                 memset(buffer, ' ', w-2);
  158.         buffer[w-2] = 0;
  159.                 mvwaddstr(win, height/2, 2, buffer);
  160.                 i = 2;
  161.             }
  162.         delay_output(300);
  163.         }
  164.  
  165.         j = 0;
  166.                                 /*  Draw running As across in RED */
  167.         init_pair(7,COLOR_RED,COLOR_GREEN);
  168.         wattron(win, COLOR_PAIR(7));
  169.     for(i=2; i < width - 4; ++i)
  170.         {   c = mvwinch(win, 4, i);
  171.             save[j++] = c;
  172.             c = c & 0x7f;
  173.         mvwaddch(win, 4, i, c);
  174.         }
  175.         wrefresh(win);
  176.  
  177.                                 /* Put a message up wait for a key */
  178.         i = height-2;
  179.         wattrset(win, COLOR_PAIR(5));
  180.     mvwaddstr(win, i, 5, " Type a key to continue or 'Q' to quit ");
  181.         wrefresh(win);
  182.  
  183.         if(WaitForUser() == 1)
  184.         break;
  185.  
  186.         j = 0;                  /* Restore the old line */
  187.         for(i=2; i < width - 4; ++i)
  188.             mvwaddch(win, 4, i, save[j++]);
  189.         wrefresh(win);
  190.  
  191.     BouncingBalls(win);
  192.                                 /* Put a message up wait for a key */
  193.         i = height-2;
  194.         wattrset(win, COLOR_PAIR(5));
  195.     mvwaddstr(win, i, 5, " Type a key to continue or 'Q' to quit ");
  196.         wrefresh(win);
  197.         if(WaitForUser() == 1)
  198.             break;
  199.     }
  200. exit:
  201.     endwin();
  202.     return 0;
  203. }
  204.  
  205. /*
  206.  * Test sub windows
  207.  */
  208. SubWinTest(WINDOW *win)
  209. {
  210. int     w, h, sw, sh, bx, by;
  211. WINDOW  *swin1, *swin2, *swin3;
  212.  
  213.     w  = win->_maxx;
  214.     h  = win->_maxy;
  215.     bx = win->_begx;
  216.     by = win->_begy;
  217.     sw = w / 3;
  218.     sh = h / 3;
  219.     if((swin1 = subwin(win, sh, sw, by+3, bx+5)) == NULL)
  220.         return  1;
  221.     if((swin2 = subwin(win, sh, sw, by+4, bx+8)) == NULL)
  222.         return  1;
  223.     if((swin3 = subwin(win, sh, sw, by+5, bx+11)) == NULL)
  224.     return  1;
  225.  
  226.     init_pair(8,COLOR_RED,COLOR_BLUE);
  227.     wattrset(swin1, COLOR_PAIR(8));
  228.     werase(swin1);
  229.     mvwaddstr(swin1, 0, 3, "Sub-window 1");
  230.     wrefresh(swin1);
  231.  
  232.     init_pair(8,COLOR_CYAN,COLOR_MAGENTA);
  233.     wattrset(swin2, COLOR_PAIR(8));
  234.     werase(swin2);
  235.     mvwaddstr(swin2, 0, 3, "Sub-window 2");
  236.     wrefresh(swin2);
  237.  
  238.     init_pair(8,COLOR_YELLOW,COLOR_GREEN);
  239.     wattrset(swin3, COLOR_PAIR(8));
  240.     werase(swin3);
  241.     mvwaddstr(swin3, 0, 3, "Sub-window 3");
  242.     wrefresh(swin3);
  243.  
  244.     delwin(swin1);
  245.     delwin(swin2);
  246.     delwin(swin3);
  247.     WaitForUser();
  248.     return  0;
  249. }
  250.  
  251. /*
  252.  *  Bouncing balls
  253.  */
  254. BouncingBalls(WINDOW *win)
  255. {
  256. chtype     c1, c2, c3;
  257. int    w, h;
  258. int     x1, y1, xd1, yd1;
  259. int     x2, y2, xd2, yd2;
  260. int     x3, y3, xd3, yd3;
  261.  
  262.     w    = win->_maxx;
  263.     h    = win->_maxy;
  264.     x1   = 2 + rand() % (w - 4);
  265.     y1   = 2 + rand() % (h - 4);
  266.     x2   = 2 + rand() % (w - 4);
  267.     y2   = 2 + rand() % (h - 4);
  268.     x3   = 2 + rand() % (w - 4);
  269.     y3   = 2 + rand() % (h - 4);
  270.     xd1  = 1; yd1 = 1;
  271.     xd2  = 1; yd2 = 0;
  272.     xd3  = 0; yd3 = 1;
  273.     nodelay(win,TRUE);
  274.     while(wgetch(win) == ERR)
  275.     {   x1 = xd1 > 0 ? ++x1 : --x1;
  276.         if(x1 <= 1 || x1 >= w - 2)
  277.             xd1 = xd1 ? 0 : 1;
  278.         y1 = yd1 > 0 ? ++y1 : --y1;
  279.         if(y1 <= 1 || y1 >= h - 2)
  280.         yd1 = yd1 ? 0 : 1;
  281.  
  282.         x2 = xd2 > 0 ? ++x2 : --x2;
  283.         if(x2 <= 1 || x2 >= w - 2)
  284.             xd2 = xd2 ? 0 : 1;
  285.         y2 = yd2 > 0 ? ++y2 : --y2;
  286.         if(y2 <= 1 || y2 >= h - 2)
  287.             yd2 = yd2 ? 0 : 1;
  288.  
  289.         x3 = xd3 > 0 ? ++x3 : --x3;
  290.         if(x3 <= 1 || x3 >= w - 2)
  291.         xd3 = xd3 ? 0 : 1;
  292.         y3 = yd3 > 0 ? ++y3 : --y3;
  293.         if(y3 <= 1 || y3 >= h - 2)
  294.             yd3 = yd3 ? 0 : 1;
  295.  
  296.         c1 = mvwinch(win, y1, x1);
  297.         c2 = mvwinch(win, y2, x2);
  298.         c3 = mvwinch(win, y3, x3);
  299.  
  300.         init_pair(8,COLOR_RED,COLOR_BLUE);
  301.         wattrset(win, COLOR_PAIR(8));
  302.     mvwaddch(win, y1, x1, 'O');
  303.         init_pair(8,COLOR_BLUE,COLOR_RED);
  304.         wattrset(win, COLOR_PAIR(8));
  305.         mvwaddch(win, y2, x2, '*');
  306.         init_pair(8,COLOR_YELLOW,COLOR_WHITE);
  307.         wattrset(win, COLOR_PAIR(8));
  308.         mvwaddch(win, y3, x3, '@');
  309.         wmove(win, 0, 0);
  310.         wrefresh(win);
  311.     mvwaddch(win, y1, x1, c1);
  312.     mvwaddch(win, y2, x2, c2);
  313.     mvwaddch(win, y3, x3, c3);
  314.     delay_output(150);
  315.     }
  316.     return 0;
  317. }
  318.  
  319. /*
  320.  *  Wait for user
  321.  */
  322. int WaitForUser()
  323. {
  324.  time_t  t;
  325.  chtype key;
  326.  
  327.  nodelay(stdscr,TRUE);
  328.  t = time((time_t *)0);
  329.  while(1)
  330.    {
  331.     if ((key = getch()) != ERR)
  332.       {
  333.        if (key  == 'q' || key == 'Q')
  334.           return  1;
  335.        else
  336.           return  0;
  337.       }
  338.     if (time((time_t *)0) - t > 5)
  339.        return  0;
  340.    }
  341. }
  342.  
  343. /*
  344.  *  Trap interrupt
  345.  */
  346. void trap()
  347. {
  348.     endwin();
  349.     exit(0);
  350. }
  351.  
  352. /*  End of DEMO.C */
  353.